package com.imagine.service.impl;

import com.imagine.entity.AccountStock;
import com.imagine.entity.BaseResult;
import com.imagine.entity.Stock;
import com.imagine.entity.vo.AccountStockVo;
import com.imagine.mapper.AccountStockMapper;
import com.imagine.service.AccountStockService;
import com.imagine.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by liangyue on 2017/10/2.
 */
@Service
public class AccountStockServiceImpl implements AccountStockService {
    @Autowired
    AccountStockMapper accountStockMapper;
    @Autowired
    StockService stockService;

    @Override
    public void insertAccountStock(AccountStock accountStock){
        accountStockMapper.insertAccountStock(accountStock);
    }


    @Override
    public void subAccountStock(AccountStock accountStock){
        accountStockMapper.subAccountStock(accountStock);

    }
    @Override
    public Object getAccountStockNumById(AccountStock accountStock){
        return accountStockMapper.getAccountStockNumById(accountStock);

    }

    @Override
    public AccountStock getAccountStockById(AccountStock accountStock){
        return accountStockMapper.getAccountStockById(accountStock);
    }

    @Override
    public void updateStockOrUsable(int remainOrUsable, String cAccountId, String operator, AccountStock accountStock){


        //remainOrUsable = 0表示更改总股票数，remainOrUsable = 1表示更改可用股票数
        if (remainOrUsable == 0){
            if (operator.equals("+")){
                accountStockMapper.insertAccountStock(accountStock);
            }else {
                accountStockMapper.subAccountStock(accountStock);
            }
        }else {
            if (operator.equals("+")){
                accountStockMapper.addAccountStockUsable(accountStock);
            }
            else {
                accountStockMapper.subAccountStockUsable(accountStock);
            }

        }
    }


    @Override
    public BaseResult getAllStockByCId(String cId){
        BaseResult result = new BaseResult();
        ArrayList<AccountStockVo> accountStockVos = new ArrayList<>();
        ArrayList<AccountStock> list = accountStockMapper.getAllStockByCId(cId);
        for (AccountStock accountStock : list){
            Stock stock = (Stock) stockService.queryStock(accountStock.getStockCode()).getObject();
            AccountStockVo vo = new AccountStockVo();
            vo.setSecurityId(accountStock.getStockCode());
            vo.setSecurityName(stock.getStockName());
            vo.setStockNum(accountStock.getStockNum());
            vo.setUsableNum(accountStock.getUsableNum());
            vo.setNewPrice(stock.getCurrentPrice());
            vo.setNewMarketValue(accountStock.getStockNum() * stock.getBidPrice());
            accountStockVos.add(vo);
        }
        result.success(accountStockVos);
        return result;
    }



}
